package com.hll.erp.controller;

import com.hll.erp.entity.Product;
import com.hll.erp.entity.ResultMap;
import com.hll.erp.entity.Sell;
import com.hll.erp.service.ProductService;
import com.hll.erp.service.SellService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * (Sell)表控制层
 *
 * @author makejava
 * @since 2021-07-04 15:18:01
 */
@RestController
@RequestMapping("sell")
@Api(
    value = "Sell Controller",
    tags = {"产品出售接口"})
public class SellController {
  /** 服务对象 */
  @Resource private SellService sellService;

  @Autowired private ProductService productService;

  @InitBinder
  public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
  }

  @RequiresRoles(value = "sell_watch")
  @ApiOperation(value = "获取出售记录,分页方式")
  @PostMapping("/getSells")
  public ResultMap getSells(
      @RequestParam(defaultValue = "1", value = "pageNo") Integer pageNo,
      @RequestParam(defaultValue = "5", value = "pageSize") Integer pageSize,
      @RequestParam(required = false) Date start,
      @RequestParam(required = false) Date end) {
    return new ResultMap()
        .success()
        .code(200)
        .message(sellService.getSells(pageNo, pageSize, start, end));
  }

  @RequiresRoles(value = "seller")
  @ApiOperation(value = "添加出售记录")
  @PostMapping("/sellProduct")
  @Transactional
  public ResultMap addSell(@ApiParam(value = "sell对象,id/sell_date任取") @RequestBody Sell sell) {
    ResultMap resultMap = new ResultMap();
    Product product = productService.getById(sell.getPid());
    boolean flag =
        product != null
            && sell.getSellNumber() != null
            && sell.getSellNumber() > 0
            && sell.getSellPrice() != null
            && sell.getSellPrice() >= 0
            && sell.getSellNumber() <= product.getNumber();
    if (flag) {
      try {
        product.setNumber(product.getNumber() - sell.getSellNumber());
        if (productService.updateById(product)) {
          sell.setSellDate(new Date());
          if (sellService.save(sell)) {
            return resultMap.success().code(200).message("出售成功");
          }
        }
      } catch (Exception e) {
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
      }
    }
    return resultMap
        .fail()
        .code(201)
        .message("出售失败,原因: 产品不存在 / 出售 < 1 / 产品剩余数量不足 / 价格 < 0 / 数据库插入失败");
  }

  @PostMapping("/getSellCount")
  public ResultMap getSellCount() {
    return new ResultMap().success().code(200).message(sellService.count());
  }
}
